package com.ecoolex.hotel.saas.platform.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.StoreWeixinConfig;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Platform, description = "商户微信配置")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = StoreWeixinConfigGraphql.AUTH,
	name = "商户微信配置",
	remark = "商户微信配置")
public class StoreWeixinConfigGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-STORE-WEIXIN-CONFIG";
	
	@Reference
	private StoreService service;
	
	private static final String AUTH_GET = AUTH + "-GET";

	private static final String AUTH_URL = AUTH + "-URL";
	
	@QueryField(field =  "storeWeixinConfig", description = "商户微信AppId")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "商户微信AppId")
	public String get(@NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		StoreWeixinConfig config = service.getStoreWeixinConfig(storeId);
		Assert.isTrue(Check.notNull(config),ResultCode.VERIFY.build(1), "商户微信配置不存在");
		return config.getAppId();
	}

	@QueryField(field =  "frontAccessUrl", description = "商户H5用户访问Url")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_URL, name = "商户H5用户访问Url")
	public String getH5Url(@NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_URL);
		StoreWeixinConfig config = service.getStoreWeixinConfig(storeId);
		Assert.isTrue(Check.notNull(config),ResultCode.VERIFY.build(1), "商户微信配置不存在");
		return config.getFrontAccessUrl();
	}
}
